package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClazzService clazzService;

    @Override
    public PageResult<Student> pageResult(StudentQueryParam student) {
        // 1.设置分页参数
        Page<Student> page = Page.of(student.getPage(), student.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        // 2.设置分页条件并执行查询
        lambdaQuery().like(student.getName() != null && !student.getName()
                                                                .isEmpty(), Student::getName,
                             student.getName())
                     .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                     .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId())
                     .page(page);


        // 3.将班级名称封装到结果中
        Set<Integer> clazzIds = page.getRecords()
                                    .stream()
                                    .map(Student::getClazzId)
                                    .collect(Collectors.toSet());

        if (!clazzIds.isEmpty()) {
            List<Clazz> clazzList = clazzService.listByIds(clazzIds);
            Map<Integer, String> clazzMap = clazzList.stream()
                                                     .collect(Collectors.toMap(Clazz::getId, Clazz::getName));
            page.getRecords()
                .forEach(stu -> {
                    stu.setClazzName(clazzMap.getOrDefault(stu.getClazzId(), null));
                });
        }

        // 4.封装结果并返回
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public void violation(Integer id, Integer score) {
        lambdaUpdate().eq(Student::getId, id)
                      .setSql("violation_count = violation_count + 1")
                      .setSql("violation_score = violation_score + " + score)
                      .set(Student::getUpdateTime, LocalDateTime.now())
                      .update();
    }
}
